package com.sky.service.admin.impl;

import com.sky.mapper.admin.AdminOrderMapper;
import com.sky.pojo.OrderDayAmountVo;
import com.sky.service.admin.AdminOrderService;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private AdminOrderMapper adminOrderMapper;

    @Override
    public BusinessDataVO businessData() {
        LocalDate now = LocalDate.now();
        List<OrderDayAmountVo> orderDayAmountVos = adminOrderMapper.orderStatistics(now, now.plusDays(1));
        if (CollectionUtils.isEmpty(orderDayAmountVos)){
            return new BusinessDataVO();
        }
        OrderDayAmountVo orderDayAmountVo = orderDayAmountVos.get(0);
        int completeNum = orderDayAmountVo.getCompleteNum();
        int totalNum = orderDayAmountVo.getTotalNum();
        double price = 0;
        if (completeNum != 0){
            price = orderDayAmountVo.getAmount().doubleValue()/completeNum ;
        }
        Integer newUser = adminOrderMapper.getNewUser(now);
        BusinessDataVO businessDataVO = BusinessDataVO.builder().validOrderCount(orderDayAmountVo.getCompleteNum())
                .turnover(orderDayAmountVo.getAmount().doubleValue())
                .orderCompletionRate((double) (completeNum / totalNum))
                .unitPrice(price)
                .newUsers(newUser).build();

        return businessDataVO;
    }

    @Override
    public DishOverViewVO dishOverView() {
        return adminOrderMapper.dishStatusCount();
    }

    @Override
    public SetmealOverViewVO setmealOverView() {
        return adminOrderMapper.setmealStatusCount();
    }
}
